﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class DaoTaoTrenDaiHoc
    {
        public String ID;
        public String maDaoTaoTrenDaiHoc;
        public String maThucTap;
        public String maNghienCuu;

        //object
        public ThucTap thucTap
        {
            get
            {
                return new ThucTap(maThucTap);
            }
            set
            {
                thucTap = value;
            }
        }
        public NghienCuu nghienCuu
        {
            get
            {
                return new NghienCuu(maNghienCuu);
            }
            set
            {
                nghienCuu = value;
            }
        }

        //database
        Database myDatabase = Database.shareDatabase;

        public DaoTaoTrenDaiHoc(ThucTap _thucTap, NghienCuu _nghienCuu)
        {
            //ID
            ID = Database.getID("DaoTaoTrenDaiHoc", "ID");
            maDaoTaoTrenDaiHoc = Database.getID("DaoTaoTrenDaiHoc", "MaDaoTaoTrenDaiHoc");

            // Object
            thucTap = _thucTap;
            nghienCuu = _nghienCuu;

        }
        public DaoTaoTrenDaiHoc(String _id)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM DaoTaoTrenDaiHoc WHERE ID = '" + _id + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                //ID
                ID = table.Rows[0]["ID"].ToString();
                maDaoTaoTrenDaiHoc = table.Rows[0]["MaTaoDaoTrenDaiHoc"].ToString();
                maThucTap = table.Rows[0]["MaThucTap"].ToString();
                maNghienCuu = table.Rows[0]["MaNghienCuu"].ToString();

            }
        }
        public Boolean Save()
        {
            String sqlTotNghiep = "INSERT INTO DaoTaoTrenDaiHoc VALUES('" 
                + ID + "','" 
                + maDaoTaoTrenDaiHoc + "','" 
                + maThucTap + "','" 
                + maNghienCuu + "')";
            QueryResult result = myDatabase.Query(sqlTotNghiep);
            if (result.ReturnCode == 409)
                return false;
            else // save successfully
            {
                thucTap.Save();
                nghienCuu.Save();

                return true;
            }


        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM DaoTaoTrenDaiHoc WHERE ID = '" + ID + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                thucTap.Delete();
                nghienCuu.Delete();
                return true;
            }
        }
    }
}
